package per.hx.bs.authorize;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import per.hx.bs.base.WxBaseController;
import per.hx.bs.base.domain.ReturnDTO;
import per.hx.bs.base.entity.EmailVO;
import per.hx.bs.entity.WxUserDO;
import per.hx.bs.entity.sys.SysUserDO;
import per.hx.bs.service.EmailService;
import per.hx.bs.service.RedisService;
import per.hx.bs.service.SysUserService;
import per.hx.bs.service.WxUserService;
import per.hx.bs.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author HuXing
 * @date 2018/5/13  19:57
 */
@RestController
@RequestMapping(value = "/wxUser")
public class WxUserController extends WxBaseController {

    private final
    SysUserService sysUserService;

    private final EmailService emailService;

    private final RedisService redisService;

    private final WxUserService wxUserService;


    @Autowired
    public WxUserController(SysUserService sysUserService, EmailService emailService, RedisService redisService, WxUserService wxUserService) {
        super(redisService);
        this.sysUserService = sysUserService;
        this.emailService = emailService;
        this.redisService = redisService;
        this.wxUserService = wxUserService;
    }

    @GetMapping
    public ReturnDTO wxUser() {
        SysUserDO sysUserDO = getSysUser();
        if (null != sysUserDO) {
            return ReturnDTO.ok(getSysUser());
        } else {
            return ReturnDTO.error(1, "未绑定用户");
        }
    }

    @GetMapping(value = "/email")
    public ReturnDTO sendCode(String email) {
        if (StringUtils.isEmpty(email)) {
            return ReturnDTO.error("邮箱获取失败");
        }
        Map<String, Object> map = new HashMap<>(1);
        map.put("email", email);
        List<SysUserDO> sysUserDOS = sysUserService.list(map);
        if (sysUserDOS.isEmpty()) {
            return ReturnDTO.error("当前邮箱不存在用户！");
        } else if (sysUserDOS.size() != 1) {
            sysUserDOS.forEach(System.out::println);
            return ReturnDTO.error("当前邮箱存在多个用户用户，请联系管理员修改");
        } else {
            EmailVO emailVO = new EmailVO();
            emailVO.setEmail(new String[]{email});
            emailVO.setSubject("登录验证码");
            int num = (int) (Math.random() * 8999) + 1000;
            emailVO.setContent("你的验证码为：" + num);
            WxUserDO wxUserDO = getWxUserDO();
            SysUserDO sysUserDO = sysUserDOS.get(0);
            sysUserDO.setStatus(num);
            redisService.set(wxUserDO.getOpenId(), sysUserDO, 30L * 60L);
            emailService.sendEmail(emailVO);
            return ReturnDTO.ok("邮件发送成功");
        }

    }

    @GetMapping(value = "/login")
    public ReturnDTO userLogin(String email, int code) {
        if (StringUtils.isEmpty(email)) {
            return ReturnDTO.error("邮箱为空！");
        }
        SysUserDO sysUserDO = (SysUserDO) redisService.get(getWxUserDO().getOpenId());
        if (code != sysUserDO.getStatus()) {
            return ReturnDTO.error("验证有误");
        } else {
            WxUserDO wxUserDO = getWxUserDO();
            wxUserDO.setUserId(sysUserDO.getUserId());
            if (wxUserService.update(wxUserDO) > 0) {
                redisService.set(getToken(), wxUserDO, 24L * 3600L);
                return ReturnDTO.ok("绑定用户成功");
            }else {
                return ReturnDTO.error();
            }

        }
    }
}
